<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/myblogs/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/myblogs/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/myblogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/myblogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/myblogs/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/myblogs/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/myblogs/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/myblogs/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/myblogs/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />





  <link rel="alternate" href="/myblogs/atom.xml" title="天行健の博客" type="application/atom+xml" />






<meta name="description" content="I do what i do!">
<meta property="og:type" content="website">
<meta property="og:title" content="天行健の博客">
<meta property="og:url" content="https://joy_24.gitee.io/index.html">
<meta property="og:site_name" content="天行健の博客">
<meta property="og:description" content="I do what i do!">
<meta property="og:locale">
<meta property="article:author" content="Joy Wang">
<meta property="article:tag" content="Java, Spring, Mybatis, SpringMvc, redis">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://joy_24.gitee.io/"/>





  <title>天行健の博客</title>
  








<meta name="generator" content="Hexo 5.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>
    <a target="_blank" rel="noopener" href="https://github.com/JoyWJ" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>


    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/myblogs/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">天行健の博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">一个开发博客的初学者。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/myblogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/myblogs/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/myblogs/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/myblogs/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/myblogs/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/myblogs/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://joy_24.gitee.io/myblogs/2021/10/25/hexo-problem-1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/myblogs/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="天行健の博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/myblogs/2021/10/25/hexo-problem-1/" itemprop="url">解决Hexo博客模板hexo-theme-next的翻页按钮不正常显示问题</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-10-25T17:30:31+08:00">
                2021-10-25
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblogs/categories/Hexo%E5%8D%9A%E5%AE%A2/" itemprop="url" rel="index">
                    <span itemprop="name">Hexo博客</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  145
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>使用Hexo搭建博客，发现翻页按钮显示不正常，显示如下：</p>
<p><img src="/myblogs/2021/10/25/hexo-problem-1/hexo-problem-1-1.png"></p>
<p>解决方法：</p>
<p>修改 themes\next\layout_partials\pagination.swig文件</p>
<p>源文件为：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;% if page.prev or page.next %&#125;</span><br><span class="line">  <span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">&quot;pagination&quot;</span>&gt;</span></span><br><span class="line">    &#123;&#123;</span><br><span class="line">      paginator(&#123;</span><br><span class="line">        prev_text: &#x27;<span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">&quot;fa fa-angle-left&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">i</span>&gt;</span>&#x27;,</span><br><span class="line">        next_text: &#x27;<span class="tag">&lt;<span class="name">i</span> <span class="attr">class</span>=<span class="string">&quot;fa fa-angle-right&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">i</span>&gt;</span>&#x27;,</span><br><span class="line">        mid_size: 1</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;&#125;</span><br><span class="line">  <span class="tag">&lt;/<span class="name">nav</span>&gt;</span></span><br><span class="line">&#123;% endif %&#125;</span><br></pre></td></tr></table></figure>

<p>修改后为：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;% if page.prev or page.next %&#125;</span><br><span class="line">  <span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">&quot;pagination&quot;</span>&gt;</span></span><br><span class="line">    &#123;&#123;</span><br><span class="line">      paginator(&#123;</span><br><span class="line">        prev_text: &#x27;<span class="tag">&lt;<span class="name">&#x27;,</span></span></span><br><span class="line"><span class="tag">        <span class="attr">next_text:</span> &#x27;&gt;</span>&#x27;,</span><br><span class="line">        mid_size: 1</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;&#125;</span><br><span class="line">  <span class="tag">&lt;/<span class="name">nav</span>&gt;</span></span><br><span class="line">&#123;% endif %&#125;</span><br></pre></td></tr></table></figure>


          
        
      
    </div>
    
    
    
    
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://joy_24.gitee.io/myblogs/2021/01/26/redis-study/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/myblogs/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="天行健の博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/myblogs/2021/01/26/redis-study/" itemprop="url">Redis教程</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-01-26T11:09:18+08:00">
                2021-01-26
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblogs/categories/Redis/" itemprop="url" rel="index">
                    <span itemprop="name">Redis</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  308
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="Redis简介"><a href="#Redis简介" class="headerlink" title="Redis简介"></a>Redis简介</h1><p>Redis 是完全开源的，遵守 BSD 协议，是一个高性能的 key-value 数据库。</p>
<p>Redis 与其他 key - value 缓存产品有以下三个特点：</p>
<ul>
<li>Redis支持数据的持久化，可以将内存中的数据保存在磁盘中，重启的时候可以再次加载进行使用。</li>
<li>Redis不仅仅支持简单的key-value类型的数据，同时还提供list，set，zset，hash等数据结构的存储。</li>
<li>Redis支持数据的备份，即master-slave模式的数据备份。</li>
</ul>
<p>Redis安装</p>
<p>windows下安装Redis</p>
<p><strong>下载地址：</strong><a target="_blank" rel="noopener" href="https://github.com/tporadowski/redis/releases%E3%80%82">https://github.com/tporadowski/redis/releases。</a></p>
<p>Redis 支持 32 位和 64 位。这个需要根据你系统平台的实际情况选择，这里我们下载 <strong>Redis-x64-xxx.zip</strong>压缩包到 C 盘，解压后，将文件夹重新命名为 <strong>redis</strong>。</p>
<p><img src="/myblogs/2021/01/26/redis-study/1-1.png"></p>
<p>解压出文件夹，内容如下</p>
<p><img src="/myblogs/2021/01/26/redis-study/1-2.png"></p>
<p>打开一个 <strong>cmd</strong> 窗口 使用 cd 命令切换目录到 <strong>C:\redis</strong> 运行：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-server.exe redis.windows.conf</span><br></pre></td></tr></table></figure>

<p>如果想方便的话，可以把 redis 的路径加到系统的环境变量里，这样就省得再输路径了，后面的那个 redis.windows.conf 可以省略，如果省略，会启用默认的。输入之后，会显示如下界面：</p>

          
        
      
    </div>
    
    
    
    
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://joy_24.gitee.io/myblogs/2021/01/19/idea-Tips/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/myblogs/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="天行健の博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/myblogs/2021/01/19/idea-Tips/" itemprop="url">idea-Tips</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-01-19T16:38:28+08:00">
                2021-01-19
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  0
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            
          
        
      
    </div>
    
    
    
    
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://joy_24.gitee.io/myblogs/2021/01/14/quartz-study/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/myblogs/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="天行健の博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/myblogs/2021/01/14/quartz-study/" itemprop="url">quartz_study</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-01-14T09:22:04+08:00">
                2021-01-14
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  0
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            
          
        
      
    </div>
    
    
    
    
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://joy_24.gitee.io/myblogs/2021/01/11/fx-ui-ListView/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/myblogs/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="天行健の博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/myblogs/2021/01/11/fx-ui-ListView/" itemprop="url">JavaFX UI之 密码框（Password Field）组件</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-01-11T15:51:45+08:00">
                2021-01-11
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblogs/categories/JavaFx/" itemprop="url" rel="index">
                    <span itemprop="name">JavaFx</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  0
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  1
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <a id="more"></a>
          
        
      
    </div>
    
    
    
    
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://joy_24.gitee.io/myblogs/2021/01/11/fx-ui-ScrollPane/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/myblogs/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="天行健の博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/myblogs/2021/01/11/fx-ui-ScrollPane/" itemprop="url">JavaFX UI之滚动面板（ScroollBar）组件</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-01-11T10:57:14+08:00">
                2021-01-11
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblogs/categories/JavaFx/" itemprop="url" rel="index">
                    <span itemprop="name">JavaFx</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.1k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  4
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <a id="more"></a>

<h4 id="创建一个滚动面板"><a href="#创建一个滚动面板" class="headerlink" title="创建一个滚动面板"></a>创建一个滚动面板</h4><p>例11-1 使用滚动面板来展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Image roses = <span class="keyword">new</span> Image(getClass().getResourceAsStream(<span class="string">&quot;roses.jpg&quot;</span>));</span><br><span class="line">ScrollPane sp = <span class="keyword">new</span> ScrollPane();</span><br><span class="line">sp.setContent(<span class="keyword">new</span> ImageView(roses));java</span><br></pre></td></tr></table></figure>

<p>setContent方法定义了滚动面板的内容节点。你只能为滚动面板的内容设置一个内容节点。如果希望创建包含多个组件的滚动面板，你可以使用布局容器或Group类。如果希望通过单击图片并移动鼠标来预览图像，你可以使用setPannable方法，并将其值设置为true。在预览时，滚动条的位置将会相应地进行改变。</p>
<h4 id="设置滚动条的显示策略"><a href="#设置滚动条的显示策略" class="headerlink" title="设置滚动条的显示策略"></a>设置滚动条的显示策略</h4><p>对于何时展现滚动条，ScrollPane类提供了不同的策略：总是展现、永不展现或者只有需要时展现。使用setHbarPolicy和setVbarPolicy方法来分别为横向和纵向滚动条指定显示策略。因此，在例11-2中，纵向滚动条将会显示出来，而横向滚动条则不会显示。</p>
<p>例11-2</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sp.setHbarPolicy(ScrollBarPolicy.NEVER);</span><br><span class="line">sp.setVbarPolicy(ScrollBarPolicy.ALWAYS);</span><br></pre></td></tr></table></figure>

<p>根据上面样例的运行结果，你只可以纵向滚动图片，如图11-2。</p>
<p><img src="/myblogs/2021/01/11/fx-ui-ScrollPane/3-11-2-scroll-pane-policy.png"></p>
<h4 id="改变滚动面板内部组件的大小"><a href="#改变滚动面板内部组件的大小" class="headerlink" title="改变滚动面板内部组件的大小"></a>改变滚动面板内部组件的大小</h4><p>在设计UI界面时，你可能会需要改变滚动面板内部组件的大小，以使其能适应滚动面板的宽和高。可以通过使用setFitToWidth或setFitToHeight方法将对应属性设置为true来在对应维度上适应大小变化。</p>
<p>在图11-3中展示的滚动面板包括了几个单选按钮、一个文本框、一个密码框。这些组件的大小超出了滚动面板预定义的范围，因此出现了一个纵向滚动条。然而，由于对滚动面板使用了setFitToWidht方法并设置其参数值为true，因此滚动面板的内容将会在水平方向上被压缩，并且永不会显示横向滚动条。</p>
<p>图11-3 自适应宽度的滚动条面板</p>
<p><img src="/myblogs/2021/01/11/fx-ui-ScrollPane/3-11-3-scroll-pane-fit.jpg"></p>
<p>在默认情况下，FIT_TO_WIDHT和FIT_TO_HEIGHT属性值都是false，并且所有的内容均会保持其初始大小。如果从代码中移除了对setFitToWidth方法的调用，那么将会看到如图11-4的效果。</p>
<p>11-4 面板内容采用自适应大小属性采用默认值</p>
<p><img src="/myblogs/2021/01/11/fx-ui-ScrollPane/3-11-4-scroll-pane-do-not-fit.jpg"></p>
<p>ScrollPane类允许你获取和设置面板内容宽度和高度的当前值、最小值、最大值。你可以在应用程序中尝试使用这些的功能。</p>
<h4 id="滚动面板样例程序"><a href="#滚动面板样例程序" class="headerlink" title="滚动面板样例程序"></a>滚动面板样例程序</h4><p>例11-3使用了一个滚动面板来展示一个带有图片的VBox。ScrollPanle类的VVALUE属性用于帮助确定当前展示的图片并显示图片文件名。</p>
<p>例11-3使用滚动面板展示图片</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javafx.application.Application;</span><br><span class="line"><span class="keyword">import</span> javafx.beans.value.ObservableValue;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.Scene;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.Label;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.ScrollPane;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.image.Image;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.image.ImageView;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.Priority;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.VBox;</span><br><span class="line"><span class="keyword">import</span> javafx.stage.Stage;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> <span class="keyword">extends</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">final</span> ScrollPane sp = <span class="keyword">new</span> ScrollPane();</span><br><span class="line">    <span class="keyword">final</span> Image[] images = <span class="keyword">new</span> Image[<span class="number">5</span>];</span><br><span class="line">    <span class="keyword">final</span> ImageView[] pics = <span class="keyword">new</span> ImageView[<span class="number">5</span>];</span><br><span class="line">    <span class="keyword">final</span> VBox vb = <span class="keyword">new</span> VBox();</span><br><span class="line">    <span class="keyword">final</span> Label fileName = <span class="keyword">new</span> Label();</span><br><span class="line">    <span class="keyword">final</span> String [] imageNames = <span class="keyword">new</span> String [] &#123;<span class="string">&quot;fw1.jpg&quot;</span>, <span class="string">&quot;fw2.jpg&quot;</span>,</span><br><span class="line">        <span class="string">&quot;fw3.jpg&quot;</span>, <span class="string">&quot;fw4.jpg&quot;</span>, <span class="string">&quot;fw5.jpg&quot;</span>&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">(Stage stage)</span> </span>&#123;</span><br><span class="line">        VBox box = <span class="keyword">new</span> VBox();</span><br><span class="line">        Scene scene = <span class="keyword">new</span> Scene(box, <span class="number">180</span>, <span class="number">180</span>);</span><br><span class="line">        stage.setScene(scene);</span><br><span class="line">        stage.setTitle(<span class="string">&quot;Scroll Pane&quot;</span>);</span><br><span class="line">        box.getChildren().addAll(sp, fileName);</span><br><span class="line">        VBox.setVgrow(sp, Priority.ALWAYS);</span><br><span class="line"></span><br><span class="line">        fileName.setLayoutX(<span class="number">30</span>);</span><br><span class="line">        fileName.setLayoutY(<span class="number">160</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">            images[i] = <span class="keyword">new</span> Image(getClass().getResourceAsStream(imageNames[i]));</span><br><span class="line">            pics[i] = <span class="keyword">new</span> ImageView(images[i]);</span><br><span class="line">            pics[i].setFitWidth(<span class="number">100</span>);</span><br><span class="line">            pics[i].setPreserveRatio(<span class="keyword">true</span>);</span><br><span class="line">            vb.getChildren().add(pics[i]);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        sp.setVmax(<span class="number">440</span>);</span><br><span class="line">        sp.setPrefSize(<span class="number">115</span>, <span class="number">150</span>);</span><br><span class="line">        sp.setContent(vb);</span><br><span class="line">        sp.vvalueProperty().addListener((ObservableValue&lt;? extends Number&gt; ov, </span><br><span class="line">            Number old_val, Number new_val) -&gt; &#123;</span><br><span class="line">                fileName.setText(imageNames[(new_val.intValue() - <span class="number">1</span>)/<span class="number">100</span>]);</span><br><span class="line">        &#125;);</span><br><span class="line">        stage.show();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        launch(args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译并运行此应用程序将会产生如图11-5所示的界面效果。</p>
<p>图11-5 效果</p>
<p><img src="/myblogs/2021/01/11/fx-ui-ScrollPane/3-11-5-scroll-pane-app.png"></p>
<p>在样例代码中ImageView对象限制图片的高为100像素。因此new_val.intValue()-1除以100的值就是在imageNames数组中当前图片的索引值。</p>
<p>在你的程序中，你可以通过改变纵向和横向滚动条的最大和最小值来动态地更新用户界面。</p>
<p>相关API文档</p>
<ul>
<li><p>ScrollPane</p>
</li>
<li><p>ScrollBar</p>
</li>
</ul>

          
        
      
    </div>
    
    
    
    
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://joy_24.gitee.io/myblogs/2021/01/11/fx-ui-ScrollBar/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/myblogs/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="天行健の博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/myblogs/2021/01/11/fx-ui-ScrollBar/" itemprop="url">JavaFX UI之滚动条（ScroollBar）组件</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-01-11T10:02:10+08:00">
                2021-01-11
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblogs/categories/JavaFx/" itemprop="url" rel="index">
                    <span itemprop="name">JavaFx</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  752
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  3
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <a id="more"></a>

<p>ScrollBar 类可以让你在应用程序中创建可滚动的面板和视图。图10-1显示了Scroll Bar的三个区域：滑块(Thumb)，左右(或上下)按钮和滚动槽(Track)。</p>
<p>图 10-1 ScrollBar元素</p>
<p><img src="/myblogs/2021/01/11/fx-ui-ScrollBar/3-10-1-scrollbar.png"></p>
<h4 id="创建Scroll-Bar"><a href="#创建Scroll-Bar" class="headerlink" title="创建Scroll Bar"></a>创建Scroll Bar</h4><p>例 10-1 创建ScrollBar</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ScrollBar sc = <span class="keyword">new</span> ScrollBar();</span><br><span class="line">sc.setMin(<span class="number">0</span>);</span><br><span class="line">sc.setMax(<span class="number">100</span>);</span><br><span class="line">sc.setValue(<span class="number">50</span>);</span><br></pre></td></tr></table></figure>

<p>其中setMin和setMax方法定义了Scroll Bar展示的最小值和最大值。当用户移动Thumb，Scroll Bar的值就会变化。例10-1中，值是50，所以当应用程序启动时，Thumb在Scroll Bar 的中间。Scroll Bar默认是水平朝向的。但是你可以通过setOrientation方法来设置其为垂直朝向。</p>
<p>用户点击左或者右按钮(垂直朝向时是上或者下)来滚动一个单位(Unit)。UNIT_INCREMENT属性指定了当一个按钮被点击时Scroll Bar调整的量。另一种方式是点击Track来滚动一个块(block)。BLOCK_INCREMENT属性定义了当Track被点击时Scroll Bar调整的量。</p>
<p>在你的应用程序中，你可以使用多种Scroll Bar中的一种来滚过超出可用空间边界的图像内容。</p>
<h4 id="应用程序中使用Scroll-Bar"><a href="#应用程序中使用Scroll-Bar" class="headerlink" title="应用程序中使用Scroll Bar"></a>应用程序中使用Scroll Bar</h4><p>在实际应用中来检验Scroll Bar。例10-2中展示的应用程序实现了一个可滚动的Scene来查看图像。这个应用程序的任务就是使用户可以查看比Scene还要高的VBox里的内容。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javafx.application.Application;</span><br><span class="line"><span class="keyword">import</span> javafx.beans.value.ChangeListener;</span><br><span class="line"><span class="keyword">import</span> javafx.beans.value.ObservableValue;</span><br><span class="line"><span class="keyword">import</span> javafx.geometry.Orientation;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.Group;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.Scene;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.ScrollBar;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.effect.DropShadow;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.image.Image;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.image.ImageView;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.VBox;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.paint.Color;</span><br><span class="line"><span class="keyword">import</span> javafx.stage.Stage;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ScrollBarSample</span> <span class="keyword">extends</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">final</span> ScrollBar sc = <span class="keyword">new</span> ScrollBar();</span><br><span class="line">    <span class="keyword">final</span> Image[] images = <span class="keyword">new</span> Image[<span class="number">5</span>];</span><br><span class="line">    <span class="keyword">final</span> ImageView[] pics = <span class="keyword">new</span> ImageView[<span class="number">5</span>];</span><br><span class="line">    <span class="keyword">final</span> VBox vb = <span class="keyword">new</span> VBox();</span><br><span class="line">    DropShadow shadow = <span class="keyword">new</span> DropShadow();</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">(Stage stage)</span> </span>&#123;</span><br><span class="line">        Group root = <span class="keyword">new</span> Group();</span><br><span class="line">        Scene scene = <span class="keyword">new</span> Scene(root, <span class="number">180</span>, <span class="number">180</span>);</span><br><span class="line">        scene.setFill(Color.BLACK);</span><br><span class="line">        stage.setScene(scene);</span><br><span class="line">        stage.setTitle(<span class="string">&quot;Scrollbar&quot;</span>);</span><br><span class="line">        root.getChildren().addAll(vb, sc);</span><br><span class="line"></span><br><span class="line">        shadow.setColor(Color.GREY);</span><br><span class="line">        shadow.setOffsetX(<span class="number">2</span>);</span><br><span class="line">        shadow.setOffsetY(<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">        vb.setLayoutX(<span class="number">5</span>);</span><br><span class="line">        vb.setSpacing(<span class="number">10</span>);</span><br><span class="line"></span><br><span class="line">        sc.setLayoutX(scene.getWidth()-sc.getWidth());</span><br><span class="line">        sc.setMin(<span class="number">0</span>);</span><br><span class="line">        sc.setOrientation(Orientation.VERTICAL);</span><br><span class="line">        sc.setPrefHeight(<span class="number">180</span>);</span><br><span class="line">        sc.setMax(<span class="number">360</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">final</span> Image image = images[i] =</span><br><span class="line">                <span class="keyword">new</span> Image(getClass().getResourceAsStream(</span><br><span class="line">                    <span class="string">&quot;fw&quot;</span> +(i+<span class="number">1</span>)+ <span class="string">&quot;.jpg&quot;</span>)</span><br><span class="line">                );</span><br><span class="line">            <span class="keyword">final</span> ImageView pic = pics[i] =</span><br><span class="line">                <span class="keyword">new</span> ImageView(images[i]);</span><br><span class="line">            pic.setEffect(shadow);</span><br><span class="line">            vb.getChildren().add(pics[i]);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        sc.valueProperty().addListener((ObservableValue&lt;? extends Number&gt; ov, </span><br><span class="line">            Number old_val, Number new_val) -&gt; &#123;</span><br><span class="line">                vb.setLayoutY(-new_val.doubleValue());</span><br><span class="line">        &#125;);  </span><br><span class="line"></span><br><span class="line">        stage.show();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        launch(args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在前几行中添加了一个包含Image的VBox和一个Scroll Bar到Scene中。</p>
<p>当Scroll Bar的VALUE属性变化时，VBox的Y坐标会相应的变化。因此当Thumb每次移动，或者按钮和Track被点击时，VBox都会移动</p>
<p>编译和运行这个应用程序，执行结果如图10-2所示。</p>
<p><img src="/myblogs/2021/01/11/fx-ui-ScrollBar/3-10-2-scroll-pics.png"></p>
<p>这个应用程序展示了ScrollBar的一种典型应用。你可以定制这个类在Scene中创建一个滚动区域。Scroll Bar可以通过应用样式来改变其默认外观，每个UI组件和Node都是这样的。</p>
<p>相关API文档</p>
<ul>
<li>ScrollBar</li>
<li>Java FX CSS Specification</li>
</ul>

          
        
      
    </div>
    
    
    
    
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://joy_24.gitee.io/myblogs/2021/01/08/fx-ui-PasswordField/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/myblogs/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="天行健の博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/myblogs/2021/01/08/fx-ui-PasswordField/" itemprop="url">JavaFX UI之 密码框（Password Field）组件</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-01-08T16:07:29+08:00">
                2021-01-08
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblogs/categories/JavaFx/" itemprop="url" rel="index">
                    <span itemprop="name">JavaFx</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  648
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  2
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <a id="more"></a>

<p>PasswordField 类实现了一个特殊的Text Field。用户输入的那些字符被显示成一个回显字符串而隐藏了。图9-1显示了一个输入了密码的Password Field。</p>
<p>图9-1</p>
<p><img src="/myblogs/2021/01/08/fx-ui-PasswordField/3-9-1-password-field-prompt.png"></p>
<h4 id="创建Password-Field"><a href="#创建Password-Field" class="headerlink" title="创建Password Field"></a>创建Password Field</h4><p>例9-1</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">PasswordField passwordField = <span class="keyword">new</span> PasswordField();</span><br><span class="line">passwordField.setPromptText(<span class="string">&quot;Your password&quot;</span>);</span><br></pre></td></tr></table></figure>

<p>对于你的用户界面，你可以在使用Password Field的同时使用提示信息或者添加一个用于提示信息的Label。和TextField 类一样，PasswordField 类也提供了setText 方法使得在应用程序启动时在组件中显示一个文本。但是，这个在setText方法中指定的字符串在Password Field中被回显字符给隐藏了。回显字符默认是小数点。图9-2显示了带有预定义文本的Password Field。</p>
<p>图9-2 设置文本的PasswordField</p>
<p><img src="/myblogs/2021/01/08/fx-ui-PasswordField/3-9-2-password-field-text.png"></p>
<p>输入到Password Field中的值可以通过getText 方法取得。你可以在你的应用程序中处理这个值，并且根据需要设置验证逻辑。</p>
<h4 id="验证密码"><a href="#验证密码" class="headerlink" title="验证密码"></a>验证密码</h4><p>例9-2 实现验证逻辑</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javafx.application.Application;</span><br><span class="line"><span class="keyword">import</span> javafx.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> javafx.geometry.Insets;</span><br><span class="line"><span class="keyword">import</span> javafx.geometry.Pos;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.Group;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.Scene;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.Label;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.PasswordField;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.HBox;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.VBox;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.paint.Color;</span><br><span class="line"><span class="keyword">import</span> javafx.stage.Stage;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PasswordFiledSample</span> <span class="keyword">extends</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line">    <span class="keyword">final</span> Label message = <span class="keyword">new</span> Label(<span class="string">&quot;&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">(Stage stage)</span> </span>&#123;</span><br><span class="line">        Group root = <span class="keyword">new</span> Group();</span><br><span class="line">        Scene scene = <span class="keyword">new</span> Scene(root, <span class="number">260</span>, <span class="number">80</span>);</span><br><span class="line">        stage.setScene(scene);</span><br><span class="line">        stage.setTitle(<span class="string">&quot;Password Field Sample&quot;</span>);</span><br><span class="line"></span><br><span class="line">        VBox vb = <span class="keyword">new</span> VBox();</span><br><span class="line">        vb.setPadding(<span class="keyword">new</span> Insets(<span class="number">10</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">10</span>));</span><br><span class="line">        vb.setSpacing(<span class="number">10</span>);</span><br><span class="line">        HBox hb = <span class="keyword">new</span> HBox();</span><br><span class="line">        hb.setSpacing(<span class="number">10</span>);</span><br><span class="line">        hb.setAlignment(Pos.CENTER_LEFT);</span><br><span class="line"></span><br><span class="line">        Label label = <span class="keyword">new</span> Label(<span class="string">&quot;Password&quot;</span>);</span><br><span class="line">        <span class="keyword">final</span> PasswordField pb = <span class="keyword">new</span> PasswordField();  </span><br><span class="line">        pb.setText(<span class="string">&quot;Your password&quot;</span>);</span><br><span class="line"></span><br><span class="line">        pb.setOnAction((ActionEvent e) -&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (!pb.getText().equals(<span class="string">&quot;T2f$Ay!&quot;</span>)) &#123;</span><br><span class="line">                message.setText(<span class="string">&quot;Your password is incorrect!&quot;</span>);</span><br><span class="line">                message.setTextFill(Color.rgb(<span class="number">210</span>, <span class="number">39</span>, <span class="number">30</span>));</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                message.setText(<span class="string">&quot;Your password has been confirmed&quot;</span>);</span><br><span class="line">                message.setTextFill(Color.rgb(<span class="number">21</span>, <span class="number">117</span>, <span class="number">84</span>));</span><br><span class="line">            &#125;</span><br><span class="line">            pb.clear();</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        hb.getChildren().addAll(label, pb);</span><br><span class="line">        vb.getChildren().addAll(hb, message);</span><br><span class="line"></span><br><span class="line">        scene.setRoot(vb);</span><br><span class="line">        stage.show();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        launch(args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Password Field的验证逻辑是使用setOnAction方法来定义的。这个方法会在密码被提交被时调用并且对输入值进行处理。如果输入的值和所需密码不同，相应的信息会用红色显示，如图9-3所示。</p>
<p>图9-3 密码不正确</p>
<p><img src="/myblogs/2021/01/08/fx-ui-PasswordField/3-9-3-password-field-incorrect.png"></p>
<p>如果输入的值和预定义的条件匹配，确认信息如图9-4所示。</p>
<p>图9-4 密码正确</p>
<p><img src="/myblogs/2021/01/08/fx-ui-PasswordField/3-9-4-password-field-correct.png"></p>
<p>从安全因素方面考虑，在值输入后清除Password Field的内容是一个最佳实践。在例9-2中，在验证被执行完成后为Password Field设置了空字符串。</p>
<p>相关API文档</p>
<ul>
<li>PasswordField</li>
<li>TextInputControl</li>
</ul>

          
        
      
    </div>
    
    
    
    
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://joy_24.gitee.io/myblogs/2021/01/08/fx-ui-TextField/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/myblogs/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="天行健の博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/myblogs/2021/01/08/fx-ui-TextField/" itemprop="url">JavaFX UI之文本框（TextField）组件</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-01-08T15:49:36+08:00">
                2021-01-08
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblogs/categories/JavaFx/" itemprop="url" rel="index">
                    <span itemprop="name">JavaFx</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1.1k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  4
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>TextField 类实现了一个接收和显示文本输入的UI组件。它提供了从用户接收文本输入的功能。这个类和另一个文本输入组件PasswordField一样，都继承自TextInput 类。TextInput 类是JavaFX<br>API中所有文本组件的超类。</p>
<p>图8-1显示了一个典型的Label加Text Field界面</p>
<p><img src="/myblogs/2021/01/08/fx-ui-TextField/3-8-1-text-field-single.png"></p>
<h4 id="创建一个Text-Field"><a href="#创建一个Text-Field" class="headerlink" title="创建一个Text Field"></a>创建一个Text Field</h4><p>例8-1中，Text Field通过和一个Label组合来指示在文本框中应该输入的内容类型。</p>
<p>例8-1</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Label label1 = <span class="keyword">new</span> Label(<span class="string">&quot;Name:&quot;</span>);</span><br><span class="line">TextField textField = <span class="keyword">new</span> TextField ();</span><br><span class="line">HBox hb = <span class="keyword">new</span> HBox();</span><br><span class="line">hb.getChildren().addAll(label1, textField);</span><br><span class="line">hb.setSpacing(<span class="number">10</span>);</span><br></pre></td></tr></table></figure>

<p>你可以如例8-1中所示的创建一个空的Text Field，或者创建一个带有指定文本数据的Text Field。要创建带有预定义文本的Text Field，使用TextField 类如下的构造函数：TextField(“Hello<br>World!”)。你可以在任何时候通过 getText 方法获取Text Field的值。</p>
<p>你可以使用TextInput类的setPrefColumnCount 方法来设置Text Field的大小，这个大小是指同一时间可以显示的最大字符个数。</p>
<h4 id="使用Text-Field构建界面"><a href="#使用Text-Field构建界面" class="headerlink" title="使用Text Field构建界面"></a>使用Text Field构建界面</h4><p>TextField 对象被用在表单中来创建多个Text Field。图8-2所示的应用程序显示了3个Text<br>Field，并处理用户在其中输入的数据。</p>
<p>图8-2</p>
<p><img src="/myblogs/2021/01/08/fx-ui-TextField/3-8-2-text-field.png"></p>
<p>代码片段例8-2创建了那三个Text Field和两个Button，然后使用GridPane 容器将他们添加到了应用程序的Scene中。这个容器在你要为UI组件实现一个灵活的布局时非常方便。</p>
<p>例8-2 添加TextField到Application</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javafx.application.Application;</span><br><span class="line"><span class="keyword">import</span> javafx.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> javafx.geometry.Insets;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.Group;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.Scene;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.Button;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.Label;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.TextField;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.GridPane;</span><br><span class="line"><span class="keyword">import</span> javafx.stage.Stage;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TextFieldSample</span> <span class="keyword">extends</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">(Stage stage)</span> </span>&#123;</span><br><span class="line">        Group root = <span class="keyword">new</span> Group();</span><br><span class="line">        Scene scene = <span class="keyword">new</span> Scene(root, <span class="number">300</span>, <span class="number">150</span>);</span><br><span class="line">        stage.setScene(scene);</span><br><span class="line">        stage.setTitle(<span class="string">&quot;Text Field Sample&quot;</span>);</span><br><span class="line"></span><br><span class="line">        GridPane grid = <span class="keyword">new</span> GridPane();</span><br><span class="line">        grid.setPadding(<span class="keyword">new</span> Insets(<span class="number">10</span>, <span class="number">10</span>, <span class="number">10</span>, <span class="number">10</span>));</span><br><span class="line">        grid.setVgap(<span class="number">5</span>);</span><br><span class="line">        grid.setHgap(<span class="number">5</span>);</span><br><span class="line"></span><br><span class="line">        scene.setRoot(grid);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//定义Name Text Field</span></span><br><span class="line">        <span class="keyword">final</span> TextField name = <span class="keyword">new</span> TextField();</span><br><span class="line">        name.setPromptText(<span class="string">&quot;Enter your first name.&quot;</span>);</span><br><span class="line">        GridPane.setConstraints(name, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">        grid.getChildren().add(name);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//定义Last Name Text Field</span></span><br><span class="line">        <span class="keyword">final</span> TextField lastName = <span class="keyword">new</span> TextField();</span><br><span class="line">        lastName.setPromptText(<span class="string">&quot;Enter your last name.&quot;</span>);</span><br><span class="line">        GridPane.setConstraints(lastName, <span class="number">0</span>, <span class="number">1</span>);</span><br><span class="line">        grid.getChildren().add(lastName);</span><br><span class="line">		</span><br><span class="line">        <span class="comment">//定义Comment Text Field</span></span><br><span class="line">        <span class="keyword">final</span> TextField comment = <span class="keyword">new</span> TextField();</span><br><span class="line">        comment.setPromptText(<span class="string">&quot;Enter your comment.&quot;</span>);</span><br><span class="line">        GridPane.setConstraints(comment, <span class="number">0</span>, <span class="number">2</span>);</span><br><span class="line">        grid.getChildren().add(comment);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//定义Submit Button</span></span><br><span class="line">        Button submit = <span class="keyword">new</span> Button(<span class="string">&quot;Submit&quot;</span>);</span><br><span class="line">        GridPane.setConstraints(submit, <span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line">        grid.getChildren().add(submit);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//定义Clear Button</span></span><br><span class="line">        Button clear = <span class="keyword">new</span> Button(<span class="string">&quot;Clear&quot;</span>);</span><br><span class="line">        GridPane.setConstraints(clear, <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line">        grid.getChildren().add(clear);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">final</span> Label label = <span class="keyword">new</span> Label();</span><br><span class="line">        GridPane.setConstraints(label, <span class="number">0</span>, <span class="number">3</span>);</span><br><span class="line">        GridPane.setColumnSpan(label, <span class="number">2</span>);</span><br><span class="line">        grid.getChildren().add(label);</span><br><span class="line"></span><br><span class="line">        submit.setOnAction((ActionEvent e) -&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (</span><br><span class="line">                    (comment.getText() != <span class="keyword">null</span> &amp;&amp; !comment.getText().isEmpty())</span><br><span class="line">                    ) &#123;</span><br><span class="line">                label.setText(name.getText() + <span class="string">&quot; &quot;</span> +</span><br><span class="line">                        lastName.getText() + <span class="string">&quot;, &quot;</span></span><br><span class="line">                        + <span class="string">&quot;thank you for your comment!&quot;</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                label.setText(<span class="string">&quot;You have not left a comment.&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        clear.setOnAction((ActionEvent e) -&gt; &#123;</span><br><span class="line">            name.clear();</span><br><span class="line">            lastName.clear();</span><br><span class="line">            comment.clear();</span><br><span class="line">            label.setText(<span class="keyword">null</span>);</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        stage.show();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        launch(args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这段代码。name，lastName和comment Text Field都是通过TextField 类的空构造方法创建的。不像例8-1，这段代码中没有伴随Text Field使用Label。替代方案是，提示说明可以提醒用户在Text Field中输入何种类型的数据。方法setPromptText定义了在应用程序启动时Text Field显示的字符串。把例8-2添加到应用程序后，其运行结果如图8-3所示。</p>
<p>图8-3 三个带有提示信息的TextField</p>
<p><img src="/myblogs/2021/01/08/fx-ui-TextField/3-8-3-text-field-prompt.png"></p>
<p>提示文本和输入在Text Field中的文本的区别是提示文本无法通过getText 方法取得。输入到Text Field中的数据会根据应用程序特定业务需求的逻辑进行处理。例8-2中，被添加到GridPane 容器中的Label 组件显示了应用程序对用户的反馈信息。当用户点击Submit按钮，setOnAction 方法检查comment Text Field。如果其包含了非空字符串，一个thank-you信息将会被显示。否则，应用程序会提示用户comment 信息还没有填写，如图8-4所示。</p>
<p>图8-4</p>
<p><img src="/myblogs/2021/01/08/fx-ui-TextField/3-8-4-text-field-nocomment.png"></p>
<p>当用户点击Clear按钮，三个Text Field中的内容都会被清除。</p>
<ul>
<li>  copy()– 将当前选中范围内的文本复制到剪切板，并保留选中的内容</li>
<li>  cut()– 将当前选中范围内的文本复制到剪切板，并移除选中的内容</li>
<li>  selectAll() – 选中Text Field中所有输入的文本</li>
<li>  paste()– 将剪切板中的内容粘贴到这个Text Field中，并替换当前选中的内容</li>
</ul>
<p>相关API文档</p>
<ul>
<li>TextField</li>
<li>TextInputControl</li>
</ul>

          
        
      
    </div>
    
    
    
    
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://joy_24.gitee.io/myblogs/2021/01/08/fx-ui-CoiceBox/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/myblogs/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="天行健の博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/myblogs/2021/01/08/fx-ui-CoiceBox/" itemprop="url">JavaFX UI 之 选择框（ChoiceBox）组件</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-01-08T14:36:05+08:00">
                2021-01-08
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblogs/categories/JavaFx/" itemprop="url" rel="index">
                    <span itemprop="name">JavaFx</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  4
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <a id="more"></a>

<p>使用ChoiceBox类添加Choice Box到你的JavaFX应用程序。其简单的实现如图7-1所示。</p>
<p>图7-1 创建带有三个选项的ChoiceBox</p>
<p><img src="/myblogs/2021/01/08/fx-ui-CoiceBox/3-7-1-choice-box.png"></p>
<h4 id="创建一个Choice-Box"><a href="#创建一个Choice-Box" class="headerlink" title="创建一个Choice Box"></a>创建一个Choice Box</h4><p>例 7-1</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ChoiceBox cb = <span class="keyword">new</span> ChoiceBox(FXCollections.observableArrayList(</span><br><span class="line">    <span class="string">&quot;First&quot;</span>, <span class="string">&quot;Second&quot;</span>, <span class="string">&quot;Third&quot;</span>)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>例7-1展示了创建一个选项列表并将其传入到ChoiceBox类的一个构造方法中。这个选项列表使用ObservableList来指定其内容。 另外，你也可以使用该类不带参数的构造方法，然后通过setItems方法设置选项列表，如例7-2所示。</p>
<p>例7-2</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ChoiceBox cb = <span class="keyword">new</span> ChoiceBox();</span><br><span class="line">cb.setItems(FXCollections.observableArrayList(</span><br><span class="line">    <span class="string">&quot;New Document&quot;</span>, <span class="string">&quot;Open &quot;</span>, </span><br><span class="line">    <span class="keyword">new</span> Separator(), <span class="string">&quot;Save&quot;</span>, <span class="string">&quot;Save as&quot;</span>)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>注意Choice Box不仅可以包含文字选项，也可以包含其它对象。例7-2中使用了一个Separator组件来分隔这些选项。集成上述代码片段到应用程序之后，其运行结果如图7-2所示。</p>
<p>图 7-2</p>
<p><img src="/myblogs/2021/01/08/fx-ui-CoiceBox/3-7-2-menu.png"></p>
<p>应用程序中，choice box被用来构建多选项的列表</p>
<h4 id="为Choice-Box设置行为"><a href="#为Choice-Box设置行为" class="headerlink" title="为Choice Box设置行为"></a>为Choice Box设置行为</h4><p>图7-3所示的应用程序中提供了一个带有5个选项的Choice Box。当选择指定的语言时，将会显示对应语言的问候。</p>
<p>图7-3</p>
<p><img src="/myblogs/2021/01/08/fx-ui-CoiceBox/3-7-3-choice-boxes.png"></p>
<p>例 7-3代码实现如何实现根据Choice Box中的被选中项来定义显示哪个问候语。</p>
<p>例7-3</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javafx.application.Application;</span><br><span class="line"><span class="keyword">import</span> javafx.beans.value.ObservableValue;</span><br><span class="line"><span class="keyword">import</span> javafx.collections.FXCollections;</span><br><span class="line"><span class="keyword">import</span> javafx.geometry.Insets;</span><br><span class="line"><span class="keyword">import</span> javafx.geometry.Pos;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.Group;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.Scene;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.ChoiceBox;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.Label;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.control.Tooltip;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.layout.HBox;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.paint.Color;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.shape.Rectangle;</span><br><span class="line"><span class="keyword">import</span> javafx.scene.text.Font;</span><br><span class="line"><span class="keyword">import</span> javafx.stage.Stage;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ChoiceBoxSample</span> <span class="keyword">extends</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line"> </span><br><span class="line">    Rectangle rect = <span class="keyword">new</span> Rectangle(<span class="number">150</span>, <span class="number">30</span>);</span><br><span class="line">    <span class="keyword">final</span> Label label = <span class="keyword">new</span> Label(<span class="string">&quot;Hello&quot;</span>);</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        launch(args);</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">(Stage stage)</span> </span>&#123;</span><br><span class="line">        Scene scene = <span class="keyword">new</span> Scene(<span class="keyword">new</span> Group());</span><br><span class="line">        scene.setFill(Color.ALICEBLUE);</span><br><span class="line">        stage.setScene(scene);</span><br><span class="line">        stage.show();</span><br><span class="line"> </span><br><span class="line">        stage.setTitle(<span class="string">&quot;ChoiceBox Sample&quot;</span>);</span><br><span class="line">        stage.setWidth(<span class="number">300</span>);</span><br><span class="line">        stage.setHeight(<span class="number">200</span>);</span><br><span class="line">      </span><br><span class="line">        label.setFont(Font.font(<span class="string">&quot;Arial&quot;</span>, <span class="number">25</span>));</span><br><span class="line">        label.setLayoutX(<span class="number">40</span>);</span><br><span class="line"> </span><br><span class="line">       <span class="keyword">final</span> String[] greetings = <span class="keyword">new</span> String[]&#123;<span class="string">&quot;Hello&quot;</span>, <span class="string">&quot;Hola&quot;</span>, <span class="string">&quot;Привет&quot;</span>, <span class="string">&quot;你好&quot;</span>,</span><br><span class="line">           <span class="string">&quot;こんにちは&quot;</span>&#125;;</span><br><span class="line">       <span class="keyword">final</span> ChoiceBox cb = <span class="keyword">new</span> ChoiceBox(FXCollections.observableArrayList(</span><br><span class="line">           <span class="string">&quot;English&quot;</span>, <span class="string">&quot;Español&quot;</span>, <span class="string">&quot;Русский&quot;</span>, <span class="string">&quot;简体中文&quot;</span>, <span class="string">&quot;日本語&quot;</span>)</span><br><span class="line">       );</span><br><span class="line">                                     </span><br><span class="line">       cb.getSelectionModel().selectedIndexProperty().addListener(</span><br><span class="line">           (ObservableValue&lt;? extends Number&gt; ov,</span><br><span class="line">                Number old_val, Number new_val) -&gt; &#123;</span><br><span class="line">                    label.setText(greetings[new_val.intValue()]);            </span><br><span class="line">        &#125;);</span><br><span class="line">             </span><br><span class="line">        cb.setTooltip(<span class="keyword">new</span> Tooltip(<span class="string">&quot;Select the language&quot;</span>));</span><br><span class="line">        cb.setValue(<span class="string">&quot;English&quot;</span>);</span><br><span class="line">        HBox hb = <span class="keyword">new</span> HBox();</span><br><span class="line">        hb.getChildren().addAll(cb, label);</span><br><span class="line">        hb.setSpacing(<span class="number">30</span>);</span><br><span class="line">        hb.setAlignment(Pos.CENTER);</span><br><span class="line">        hb.setPadding(<span class="keyword">new</span> Insets(<span class="number">10</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">10</span>));</span><br><span class="line"> </span><br><span class="line">        ((Group) scene.getRoot()).getChildren().add(hb);                       </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>ChangeListener<Number>对象通过连续调用getSelectionModel 和selectedIndexProperty方法得到当前被选项所索引。 其中getSelectionModel方法返回被选项， 而selectedIndexProperty方法返回变量名为cb 的ChoiceBox 的SELECTED_INDEX 属性。最后，其整数值作为索引可以从greetings 数组中找到一个String元素，并将label的文本设置为此String。例如，如果用户选择了第二个选项，其对应的语言是Spanish，则SELECTED_INDEX属性等于1，而从greetings数组中会找到“Hola”，然后label就会显示为“Hola”。</Number></p>
<p>你可以指定一个提示信息(tooltip)让ChoiceBox 组件富含更多的信息。Tooltip是javafx.scene.control包中的一个UI组件。它可以被应用到任何JavaFX UI控件之上。</p>
<h4 id="应用提示信息-Tooltip"><a href="#应用提示信息-Tooltip" class="headerlink" title="应用提示信息(Tooltip)"></a>应用提示信息(Tooltip)</h4><p>Tooltip 类提供了一个预定义的提示信息，它可以通过setTooltip方法很方便地应用到Choice<br>Box(或者任何其它控件)之上，如例7-4。</p>
<p>例7-4 为ChoiceBox天津Tooltip</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cb.setTooltip(<span class="keyword">new</span> Tooltip(<span class="string">&quot;Select the language&quot;</span>));</span><br></pre></td></tr></table></figure>

<p>一般来说，用户定义Tooltip文本的方式是将文本直接传入Tooltip类的一个构造方法中。不过如果你的应用程序逻辑需要UI动态地设置文本，你可以使用不带参数的构造方法先创建一个Tooltip ，然后通过setText 方法为其设置文本。</p>
<p>在为变量名为cb代表的ChoiceBox应用了Tooltip之后，用户将鼠标放置在Choice Box之上就会看到如图7-5所示的界面。</p>
<p><img src="/myblogs/2021/01/08/fx-ui-CoiceBox/3-7-5-choice-box-tooltip.png"></p>
<p>如果要进一步改进你的应用程序，你可以使用CSS来为Choice Box增加样式，或者对ChoiceBox应用视觉特效或变换。</p>
<p>相关API文档</p>
<ul>
<li>ChoiceBox</li>
<li>Tooltip</li>
</ul>

          
        
      
    </div>
    
    
    
    
    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/myblogs/page/2/">2</a><span class="space">&hellip;</span><a class="page-number" href="/myblogs/page/4/">4</a><a class="extend next" rel="next" href="/myblogs/page/2/">&gt;</a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/myblogs/uploads/avatar.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                #<a href="/myblogs/archives%7C%7C%20archive">
                <a href="/myblogs/archives">
              
                  <span class="site-state-item-count">34</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/myblogs/categories/index.html">
                  <span class="site-state-item-count">9</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/myblogs/tags/index.html">
                  <span class="site-state-item-count">20</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/myblogs/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/joyWJ" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="wangjianjoy@126.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Joy Wang</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/myblogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/myblogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/myblogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/myblogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/myblogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/myblogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/myblogs/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/myblogs/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/myblogs/js/src/motion.js?v=5.1.4"></script>



  
  

  

  


  <script type="text/javascript" src="/myblogs/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/myblogs/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
